package cn.edu.ptu.jobfairs.services.impl;

import cn.edu.ptu.jobfairs.basic.bo.MemberPos;
import cn.edu.ptu.jobfairs.basic.bo.Position;
import cn.edu.ptu.jobfairs.basic.bo.Resume;
import cn.edu.ptu.jobfairs.basic.common.DataResult;
import cn.edu.ptu.jobfairs.basic.common.Result;
import cn.edu.ptu.jobfairs.basic.common.User;
import cn.edu.ptu.jobfairs.basic.dto.request.IdDTO;
import cn.edu.ptu.jobfairs.basic.enums.MemberStatus;
import cn.edu.ptu.jobfairs.core.SqlHandler;
import cn.edu.ptu.jobfairs.core.utils.SqlBuilder;
import cn.edu.ptu.jobfairs.core.utils.UserUtils;
import cn.edu.ptu.jobfairs.core.utils.ValidUtils;
import cn.edu.ptu.jobfairs.services.PositionService;
import org.springframework.security.access.prepost.PostAuthorize;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;

import static cn.edu.ptu.jobfairs.basic.common.Errors.INVALID_PARAMS;
import static cn.edu.ptu.jobfairs.basic.common.Errors.NOT_LOGGED;

/**
 * Created by Lin Chenxiao on 2020-08-25
 **/
@Service
@Transactional
public class PositionServiceImpl implements PositionService {
    @Resource
    private SqlHandler sqlHandler;

    @Override
    @PostAuthorize("hasAuthority('admin')")
    public Result addPosition(Position position) {
        if (position == null || StringUtils.isEmpty(position.getName())) return Result.fail(INVALID_PARAMS);
        sqlHandler.notExists(SqlBuilder.from("select count(*) from # where # limit 1")
                .append(Position.class).where("name", position.getName()));
        Integer id = sqlHandler.create(SqlBuilder.from("insert into # #").append(Position.class).insert(position));
        return DataResult.ok(id);
    }

    @Override
    @PostAuthorize("hasAuthority('admin')")
    public Result editPosition(Position position) {
        if (ValidUtils.hasBlank(position)) return Result.fail(INVALID_PARAMS);
        sqlHandler.notExists(SqlBuilder.from("select count(*) from # where # and id != # limit 1")
                .append(Position.class).where("name", position.getName()).append(position.getId()));
        sqlHandler.updateOne(SqlBuilder.from("update # set # where # limit 1")
                .append(Position.class).update(position).where(position.getId()));
        return Result.OK;
    }

    @Override
    @PostAuthorize("hasAuthority('admin')")
    public Result delPosition(IdDTO idDTO) {
        if (ValidUtils.hasBlank(idDTO)) return Result.fail(INVALID_PARAMS);
        sqlHandler.notExists(SqlBuilder.from("select count(1) from # where #").append(Resume.class)
                .where("positionId", idDTO.getId()));
        sqlHandler.deleteOne(SqlBuilder.from("delete from # where # limit 1")
                .append(Position.class).where(idDTO.getId()));
        sqlHandler.delete(SqlBuilder.from("delete from # where #").append(MemberPos.class)
                .where("positionId", idDTO.getId()));
        return Result.OK;
    }

    @Override
    @PostAuthorize("hasAuthority('admin')")
    public Result listPosition() {
        return DataResult.ok(sqlHandler.selectAll(SqlBuilder.from("select * from #")
                .append(Position.class), Position.class));
    }

    @Override
    public Result listAuthPosition() {
        User user = UserUtils.getUser();
        if (user == null) return Result.fail(NOT_LOGGED);
        if (ValidUtils.intEquals(user.getStatus(), MemberStatus.USER.getValue()))
            return DataResult.ok(sqlHandler.selectAll(
                    SqlBuilder.from("select * from # right join # on # = # where # = #")
                            .append(Position.class).append(MemberPos.class)
                    .append(Position.class, "id").append(MemberPos.class, "positionId")
                            .append(MemberPos.class, "memberId").append(user.getId())
                    , Position.class));
        else return DataResult.ok(sqlHandler.selectAll(SqlBuilder.from("select * from #")
                .append(Position.class), Position.class));
    }
}
